﻿#if NET7_0_OR_GREATER
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

using zijian666.Converts.Abstractions;
using zijian666.Converts.Convertor.Extra;
using zijian666.Converts.Core;
using zijian666.Converts.Extensions;

namespace zijian666.Converts.Factory;
/// <summary>
/// 
/// </summary>
public sealed class ParsableConvertorFactory : IConvertorFactory
{
    public MatchedConvertor<T> Create<T>()
    {
        var type = typeof(T);
        var genericArgs = type.GetGenericArguments(typeof(ISpanParsable<>));
        if (genericArgs is not null)
        {
            var matchedType = typeof(ISpanParsableConvertor<>).MakeGenericType(type);
            var convertor = (IConvertor<T>)Activator.CreateInstance(matchedType)!;
            return new MatchedConvertor<T>(convertor, MacthedType.Equals);
        }

        genericArgs = type.GetGenericArguments(typeof(IParsable<>));
        if (genericArgs is not null)
        {
            var matchedType = typeof(ISpanParsableConvertor<>).MakeGenericType(type);
            var convertor = (IConvertor<T>)Activator.CreateInstance(matchedType)!;
            return new MatchedConvertor<T>(convertor, MacthedType.Equals);
        }

        return default;
    }
}

#endif